//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "NormalDistribution.h"
using namespace Cephei::QL::Math::Distributions;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (Microsoft::FSharp::Core::FSharpOption<Double>^ average, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phNormalDistribution = NULL;
#endif
        QuantLib::Real _average = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (average) ? (QuantLib::Real)ValueHelper::Convert (average->Value) : 0.0); //4
        QuantLib::Real _sigma = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (sigma) ? (QuantLib::Real)ValueHelper::Convert (sigma->Value) : 1.0); //4
        _ppNormalDistribution = new boost::shared_ptr<QuantLib::NormalDistribution> (new QuantLib::NormalDistribution ( _average,  _sigma ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (boost::shared_ptr<QuantLib::NormalDistribution>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNormalDistribution = NULL;
#endif
	_ppNormalDistribution = &childNative;
    
}
Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (QuantLib::NormalDistribution& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phNormalDistribution = NULL;
#endif
	_ppNormalDistribution = new boost::shared_ptr<QuantLib::NormalDistribution> (&childNative);
    
    _NormalDistributionOwner = owner;
    
}

Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (CNormalDistribution^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNormalDistribution = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppNormalDistribution = new boost::shared_ptr<QuantLib::NormalDistribution> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNormalDistribution = NULL;
#endif
	if (!t->IsSubclassOf(CNormalDistribution::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (QuantLib::Handle<QuantLib::NormalDistribution>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phNormalDistribution = &childNative;
	_ppNormalDistribution = &static_cast<boost::shared_ptr<QuantLib::NormalDistribution>>(childNative.currentLink());
    
    _NormalDistributionOwner = owner;
}
Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (QuantLib::Handle<QuantLib::NormalDistribution> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phNormalDistribution = &childNative;
	_ppNormalDistribution = &static_cast<boost::shared_ptr<QuantLib::NormalDistribution>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Distributions::CNormalDistribution::CNormalDistribution (QuantLib::NormalDistribution childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phNormalDistribution = NULL;
#endif
	_ppNormalDistribution = new boost::shared_ptr<QuantLib::NormalDistribution> (new QuantLib::NormalDistribution (childNative));
    
}
#endif

Cephei::QL::Math::Distributions::CNormalDistribution::~CNormalDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppNormalDistribution != NULL)
    {
	    delete _ppNormalDistribution;
        _ppNormalDistribution = NULL;
    }
}
Cephei::QL::Math::Distributions::CNormalDistribution::!CNormalDistribution ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppNormalDistribution != NULL)
    {
	    delete _ppNormalDistribution;
    }
}
QuantLib::NormalDistribution& Cephei::QL::Math::Distributions::CNormalDistribution::GetReference ()
{
    if (_ppNormalDistribution == NULL) throw gcnew NativeNullException ();
	return **_ppNormalDistribution;
}
boost::shared_ptr<QuantLib::NormalDistribution>& Cephei::QL::Math::Distributions::CNormalDistribution::GetShared ()
{
    if (_ppNormalDistribution == NULL) throw gcnew NativeNullException ();
	return *_ppNormalDistribution;
}
QuantLib::NormalDistribution* Cephei::QL::Math::Distributions::CNormalDistribution::GetPointer ()
{
    if (_ppNormalDistribution == NULL) throw gcnew NativeNullException ();
	return &**_ppNormalDistribution;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::NormalDistribution>& Cephei::QL::Math::Distributions::CNormalDistribution::GetHandle ()
{
	if (_phNormalDistribution == NULL)
	{
		_phNormalDistribution = new Handle<QuantLib::NormalDistribution> (*_ppNormalDistribution);
	}
	return *_phNormalDistribution;
}
#endif
bool Cephei::QL::Math::Distributions::CNormalDistribution::HasNative () 
{
	return (_ppNormalDistribution != NULL);
}

Double Cephei::QL::Math::Distributions::CNormalDistribution::Derivative (Double x)
{
    try
    {
        QuantLib::Real _x = (QuantLib::Real)ValueHelper::Convert (x);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppNormalDistribution)->derivative ( _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Distributions::INormalDistribution^ Cephei::QL::Math::Distributions::CNormalDistribution_Factory::Create (Microsoft::FSharp::Core::FSharpOption<Double>^ average, Microsoft::FSharp::Core::FSharpOption<Double>^ sigma)
{
    return gcnew CNormalDistribution ( average,  sigma);
}
